package controller;

import dao.EmentaJpaController;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import dao.exceptions.NonexistentEntityException;
import model.Disciplina;
import model.DisciplinaProfessor;
import model.Ementa;
import pagecontroller.JsfUtil;
import pagecontroller.PageManager;

/**
 *
 * @author Edilson Fernandes da Costa
 * @version 1.0
 */
public class EmentaController extends Controller {

    public EmentaController() {
        if (JsfUtil.getObjectFromSession("EmentaJpaController") == null) {
            jpaController = new EmentaJpaController();
        } else {
            jpaController = (EmentaJpaController) JsfUtil.getObjectFromSession("EmentaJpaController");
        }
    }
    private Ementa ementa = null;
    private Ementa ementaAux = null;
    private EmentaJpaController jpaController = null;
    private List<Ementa> ementas = null;
    private Disciplina disciplina;
    private DisciplinaProfessor disciplinaProfessor;

    public String persiste() {
        if (ementa.getId() == null) {
            return create();
        } else {
            return update();
        }
    }

    public String update() {
        try {
            jpaController.edit(this.ementa);
            ementaAux = ementa;
            this.ementa = null;
            JsfUtil.addSuccessMessage("Ementa Atualizado com Sucesso");
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(EmentaController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(EmentaController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "refreshPage";
    }

    public String create() {
        try {
            jpaController.create(ementa);
            ementaAux = ementa;
            ementa = null;
            JsfUtil.addSuccessMessage("Ementa cadastrado com sucesso.");
            getPageManager().setPageEmentaRelat();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "Ocorreu um erro na percistencia dos dados.");
            return null;
        }
        return "refreshPage";
    }

    public String edit() {
        Ementa ementaTemp = (Ementa) JsfUtil.getObjectFromRequestParameter("ementa");
        this.ementa = ementaTemp;
        return "refreshPage";
    }

    public String destroy() {
        Ementa ementaTemp = (Ementa) JsfUtil.getObjectFromRequestParameter("ementa");
        try {
            jpaController.destroy(ementaTemp.getId());
            JsfUtil.addSuccessMessage("Ementa removido com sucesso.");
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(EmentaController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "refreshPage";
    }

    public String destroyButton() {
        Ementa ementaTemp = ementaAux;
        try {
            jpaController.destroy(ementaTemp.getId());
            JsfUtil.addSuccessMessage("Ementa removida com sucesso.");
            ementaAux = null;

        } catch (NonexistentEntityException ex) {
            Logger.getLogger(AlunoController.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "refreshPage";
    }

    public String load() {
        ementaAux = (Ementa) JsfUtil.getObjectFromRequestParameter("ementaTemp");
        getPageManager().setModalEmentaRelat();
        return "refreshPage";
    }

    public String editButton() {
        this.ementa = ementaAux;
        getPageManager().setPageEmentaCad();
        return "refreshPage";
    }

    public Ementa getEmenta() {
        if (ementa == null) {
            ementa = new Ementa();
            ementa.setDisciplinaProfessor(getDisciplinaProfessor());
        }
        return ementa;
    }

    public List<Ementa> getEmentas() {
        this.ementas = jpaController.findEmentaEntities();
        return this.ementas;
    }

    public EmentaJpaController getJpaController() {
        return jpaController;
    }

    public Ementa getEmentaAux() {
        return ementaAux;
    }

//    public Disciplina getDisciplina() {
//        if (disciplina == null) {
//            DisciplinaController disciplinaController = (DisciplinaController) JsfUtil.getObjectFromSession("DisciplinaController");
//            if (disciplinaController == null) {
//                disciplinaController = new DisciplinaController();
//            }
//            disciplina = disciplinaController.getDisciplinaAux();
//        }
//        return disciplina;
//    }
//
//    public void setDisciplina(Disciplina disciplina) {
//        this.disciplina = disciplina;
//    }

    public DisciplinaProfessor getDisciplinaProfessor() {
        if (disciplinaProfessor == null) {
            getDisciplinaController().getDisciplinaProfessor();
        }
        if(disciplinaProfessor == null){
            disciplinaProfessor =  new DisciplinaProfessor();
        }
        return disciplinaProfessor;
    }

    public void setDisciplinaProfessor(DisciplinaProfessor disciplinaProfessor) {
        this.disciplinaProfessor = disciplinaProfessor;
    }

    public List<Ementa> finEmentas(DisciplinaProfessor disciplinaProfessor) {
        return jpaController.findEmenta(disciplinaProfessor);
    }
}
